﻿using CefSharp;
using NLog;

namespace CefWebrtcDemo.wpf
{
    public class MyPermissionHandler : IPermissionHandler
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public bool OnRequestMediaAccessPermission(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame,
            string requestingOrigin, MediaAccessPermissionType requestedPermissions, IMediaAccessCallback callback)
        {
            _logger.Debug("> OnRequestMediaAccessPermission : {0} ({1})", requestedPermissions, requestingOrigin);

            
            //默认允许所有请求
            callback.Continue(requestedPermissions);
            return true;
            
            // var handled = false;
            // if (requestedPermissions.HasFlag(MediaAccessPermissionType.AudioCapture))
            // {
            //     callback.Continue(MediaAccessPermissionType.AudioCapture);
            //     handled = true;
            // }
            //
            // if (requestedPermissions.HasFlag(MediaAccessPermissionType.VideoCapture))
            // {
            //     callback.Continue(MediaAccessPermissionType.VideoCapture);
            //     handled = true;
            // }
            //
            // if (requestedPermissions.HasFlag(MediaAccessPermissionType.DesktopAudioCapture))
            // {
            //     callback.Continue(MediaAccessPermissionType.DesktopAudioCapture);
            //     handled = true;
            // }
            //
            // if (requestedPermissions.HasFlag(MediaAccessPermissionType.DesktopVideoCapture))
            // {
            //     callback.Continue(MediaAccessPermissionType.DesktopVideoCapture);
            //     handled = true;
            // }
            // return true;

            // using (callback)
            //     return false;
        }

        public bool OnShowPermissionPrompt(IWebBrowser chromiumWebBrowser, IBrowser browser, ulong promptId,
            string requestingOrigin,
            PermissionRequestType requestedPermissions, IPermissionPromptCallback callback)
        {
            _logger.Debug("> OnShowPermissionPrompt : {0} ({1})", requestedPermissions, promptId);
            using (callback)
                return false;
        }

        public void OnDismissPermissionPrompt(IWebBrowser chromiumWebBrowser, IBrowser browser, ulong promptId,
            PermissionRequestResult result)
        {
            _logger.Debug("> OnDismissPermissionPrompt : {0} ({1})", result, promptId);
        }
    }
}